﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TekaMUD.Engine.Persistence;

namespace TekaMUD.Engine.Blocks
{
	/// <summary>
	/// The biggest mass of land existing within the game. It's composed of a tile matrix.
	/// Each tile may have a number of rooms.
	/// </summary>
	public class Continent : GameEntity
	{
		public int Grid_x_size { get; private set; }
		public int Grid_y_size { get; private set; }
		public string Name { get; private set; }
		
		private Tile[,] _tilesArray;
		private List<Sector> Sectors;
		public EnvData Environment { get; set; }

		public Continent(string name, int xSize, int ySize)
		{
			Grid_x_size = xSize;
			Grid_y_size = ySize;
			Name = name;
	
			_tilesArray = new Tile[xSize, ySize];
			Sectors = new List<Sector>();
			SyncRoot = new object();
		}

		public void AddSector(Sector s)
		{
			lock (SyncRoot)
			{
				if (Sectors.Contains(s))
					throw new UserException("Sector " + s.Name + " already exists in this Continent (" + this.Name + ")");


				Sectors.Add(s);
				s.Continent = this;
			}
		}

		public void AddSector(List<Sector> sectors)
		{
			lock (SyncRoot)
			{
				foreach (Sector s in sectors)
				{
					if (Sectors.Contains(s))
						throw new UserException("Sector " + s.Name + " already exists in this Continent (" + this.Name + ")");
					
					Sectors.Add(s);
					s.Continent = this;
				}
			}
		}

		public Sector GetFirstSector()
		{
			lock (SyncRoot)
			{
				return Sectors[0];
			}
		}

		public override string ToString()
		{
			return UID.ToString("n") + " - " + Name;
		}
	}
}
